package logger

import (
	"fmt"
	"go.uber.org/zap"
	"gorm.io/gorm/logger"
	"time"
)

type GormLoggerWriter struct {
	ctxLogger *zap.SugaredLogger
}

// Printf 自定义写入方法
func (g *GormLoggerWriter) Printf(s string, i ...interface{}) {
	g.ctxLogger.Desugar().Log(SqlLevel, fmt.Sprintf(s, i...))
}

func NewGormLoggerWriter(ctxLogger *zap.SugaredLogger) *GormLoggerWriter {
	return &GormLoggerWriter{
		ctxLogger: ctxLogger,
	}
}

func NewGormLogger(ctxLogger *zap.SugaredLogger) logger.Interface {
	gormLogger := logger.New(
		NewGormLoggerWriter(ctxLogger),
		logger.Config{
			SlowThreshold:             200 * time.Millisecond,
			LogLevel:                  logger.Info,
			IgnoreRecordNotFoundError: false,
			Colorful:                  false,
		},
	)
	return gormLogger
}
